<?php
require_once('Vega/Dao/Table.php');

class Mobitv_Admin_GroupDao extends Vega_Dao_Table
{
    // Ham lay danh sach menu va quyen cua nhom doi voi moi menu
    public function getMenuList($groupId) {
        $arrParentMenu = array();
        $sql = "SELECT m.*, agm.permission FROM menu m LEFT JOIN admin_group_menu agm ON m.id=agm.menu_id AND agm.group_id=$groupId";
        $this->_db->query($sql);
        $rows = $this->_db->fetchRowSet();
        foreach ($rows as $row) {
            $parentId = $row['parent_id'];
            $id = $row['id'];
            if ($parentId == 0) {
                $arrParentMenu['p' . $id] = array();
                $arrParentMenu['p' . $id]['self'] = $row;
                $arrParentMenu['p' . $id]['children'] = array();
            } else {
                continue;
            }
        }

        foreach ($rows as $row) {
            $parentId = $row['parent_id'];
            if ($parentId == 0) continue;
            if (isset($arrParentMenu['p' . $parentId])) {
                $arrParentMenu['p' . $parentId]['children'][] = $row;
            }

        }

        return $arrParentMenu;
    }

    public function saveMenu($groupId, $menuId, $perm) {
        $groupId = intval($groupId);
        $perm = intval($perm);
        $sql = "INSERT INTO admin_group_menu SET permission=$perm, group_id=$groupId, menu_id=$menuId ON DUPLICATE KEY UPDATE permission=$perm";
        return $this->_db->queryWrite($sql);
    }

    public function getDetail($groupId)
    {
        $groupId = intval($groupId);
        $sql = "SELECT * FROM admin_group WHERE id=$groupId";
        $this->_db->query($sql);

        return $this->_db->fetchRow();
    }
    
    public function getList()
	{
		$sql = "SELECT * FROM admin_group ORDER BY id ASC";
        $this->_db->query($sql);
        return $this->_db->fetchRowset();
	}

    public function getPermissionList($groupId)
    {
        $groupId = intval($groupId);
        $sql = "SELECT * FROM admin_action LEFT JOIN (SELECT * FROM admin_group_role WHERE group_id=$groupId) AS admin_group_role ON admin_action.id=admin_group_role.action_id";
        $this->_db->query($sql);
        
        return $this->_db->fetchRowset();
    }

    public function savePermission($groupId, $actionId, $perm)
    {
        $action = $this->_db->valueQuote($actionId);
        $groupId = intval($groupId);
        $perm = intval($perm);

        $sql = "INSERT INTO admin_group_role SET permission=$perm, group_id=$groupId, action_id=$actionId ON DUPLICATE KEY UPDATE permission=$perm";

        return $this->_db->queryWrite($sql);
    }

    public function save($groupId, $data)
    {
        $groupId = intval($groupId);
        $name = $this->_db->valueQuote($data['name']);
        $description = $this->_db->valueQuote($data['description']);

        $sql = "UPDATE admin_group SET name=$name, description=$description WHERE id=$groupId";
        
        return $this->_db->queryWrite($sql);
    }

    public function insert($data)
    {
        $name = $this->_db->valueQuote($data['name']);
        $description = $this->_db->valueQuote($data['description']);

        $sql = "INSERT INTO admin_group SET name=$name, description=$description";

        return $this->_db->queryWrite($sql);
    }

    public function deleteFromList($list)
    {
        $list = "(" . implode(",", $list) . ")";
        
        $sql = "DELETE FROM admin_group WHERE deletable=1 AND id IN $list";

        return $this->_db->queryWrite($sql);
    }
}
?>